package com.china08.yxyapi.api.home.kecheng.statistical;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;

@CrossOrigin
@RestController
public class CloudSchoolLessonListController extends YxyApiBaseController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/api_yw/home/kecheng/cloud/lesson/school/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String OrderBy = reqModel.OrderBy;
        String AscOrDesc = reqModel.AscOrDesc;
        if ("ascending".equals(AscOrDesc)) {
            AscOrDesc = "asc";
        } else {
            AscOrDesc = "desc";
        }

        String keyword = StringUtils.isNotBlank(reqModel.keyword) ? "%" + reqModel.keyword + "%" : "";
        String areaIdDistrict = reqModel.areaIdDistrict;
        Integer pageNum = reqModel.pageNum;

        List<Object> paramList = new ArrayList<Object>();

        StringBuffer sb1 = new StringBuffer();
        sb1.append("  select                                                        ");
        sb1.append("    count(school_id)                                            ");
        sb1.append("  from yxy_lessons.yxy_lessons_cloud_report_school              ");
        sb1.append("      where 1 = 1                                               ");
        if (StringUtils.isNotBlank(keyword)) {
            sb1.append("        and school_name like ?                              ");
            paramList.add(keyword);
        }
        if (StringUtils.isNotBlank(areaIdDistrict)) {
            sb1.append("        and area_id_district = ?                                 ");
            paramList.add(areaIdDistrict);
        }
        Integer allSize = jdbcTemplate.queryForObject(sb1.toString(), paramList.toArray(), Integer.class);

        StringBuffer sb2 = new StringBuffer();
        sb2.append("  select                                                        ");
        sb2.append("    school_name,                                                ");
        sb2.append("    lessons_count,                                              ");
        sb2.append("    pass_count,                                                 ");
        sb2.append("    high_quality_count                                          ");
        sb2.append("  from yxy_lessons.yxy_lessons_cloud_report_school              ");
        sb2.append("      where 1 = 1                                               ");
        if (StringUtils.isNotBlank(keyword)) {
            sb2.append("        and school_name like ?                              ");
        }
        if (StringUtils.isNotBlank(areaIdDistrict)) {
            sb2.append("        and area_id_district = ?                            ");
        }
        sb2.append("  order by " + OrderBy + " " + AscOrDesc + "                    ");
        sb2.append("  limit ? ,?                                                    ");
        paramList.add((pageNum - 1) * 10);
        paramList.add(10);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sb2.toString(), paramList.toArray());

        List<Pojo> returnList = new ArrayList<Pojo>();
        for (Map<String, Object> map : list) {
            Pojo pojo = new Pojo();
            pojo.schoolName = map.get("school_name") + "";
            try {
                pojo.lessons_count = Integer.parseInt(map.get("lessons_count") + "");
            } catch (Exception e) {
            }
            try {
                pojo.pass_count = Integer.parseInt(map.get("pass_count") + "");
            } catch (Exception e) {
            }
            try {
                pojo.high_quality_count = Integer.parseInt(map.get("high_quality_count") + "");
            } catch (Exception e) {
            }
            returnList.add(pojo);
        }
        RespModel respModel = new RespModel();
        respModel.allSize = allSize;
        respModel.list = returnList;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String OrderBy;
        public String AscOrDesc;
        public String keyword;
        public String areaIdDistrict;
        public Integer pageNum;

        @Override
        public void autoCheck() {
        }
    }

    public static class Pojo {
        public String schoolName;
        public Integer lessons_count = 0;
        public Integer pass_count = 0;
        public Integer high_quality_count = 0;
    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allSize;
        public List<Pojo> list;
    }
}
